package com.zifeng.task.schedule;

import com.alibaba.fastjson.JSON;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.zifeng.common.Constant;
import com.zifeng.common.R;
import com.zifeng.dao.UserDao;
import com.zifeng.feign.AssignmentFeignClient;
import com.zifeng.pojo.Assignment;
import com.zifeng.pojo.User;
import com.zifeng.service.impl.UserServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.*;
import java.util.stream.Collectors;

//@Component
public class AssignmentTask {

    @Autowired
    private AssignmentFeignClient assignmentFeignClient;

    @Autowired
    private UserDao userDao;

    @Autowired
    private UserServiceImpl userService;

    //每3秒执行一次，扫描已完成的委托
    @Scheduled(fixedDelay = 3000)
    private void assginmentScan() {

        // 1. 扫描任务委托表中所有委托，获取已完成的委托
        R res = assignmentFeignClient.getFinishedAssignments();
        String jsonStr = (String) res.get("finishedList");

        List<Assignment> finishedList = JSON.parseArray(jsonStr, Assignment.class);
        if (finishedList.isEmpty()) {
            return;
        }
        List<Integer> masterIDList = finishedList.stream().map(item -> item.getMasterId()).collect(Collectors.toList());
        List<Integer> clientIDList = finishedList.stream().map(item -> item.getClientId()).collect(Collectors.toList());

        // 2. 扣除委托方账户余额，增加完成委托方账户余额
        Map<Integer, Long> userIdToMoney = new HashMap<>(); // 用户ID和对应委托价格的映射表
        for (Assignment assignment : finishedList) {
            userIdToMoney.put(assignment.getMasterId(), userIdToMoney.getOrDefault(assignment.getMasterId(), 0L) - assignment.getPrice());
            userIdToMoney.put(assignment.getClientId(), userIdToMoney.getOrDefault(assignment.getClientId(), 0L) + assignment.getPrice());
        }
        List<User> masterList = userDao.selectBatchIds(masterIDList);
        List<User> clientList = userDao.selectBatchIds(clientIDList);
        HashSet<User> userHashSet = new HashSet<>();
        userHashSet.addAll(masterList);
        userHashSet.addAll(clientList);
        for (User user : userHashSet) {
            Long change = userIdToMoney.getOrDefault(Integer.parseInt(String.valueOf(user.getId())), 0L);
            user.setMoney(user.getMoney() + change);
        }
        ArrayList<User> userResList = new ArrayList<>(userHashSet);
        for (User user : userResList) {
            userDao.updateById(user);
        }

        // 3. 将已经处理好的委托转成状态：1->2 表示已经处理, 0：初始状态，1：已完成，2：已经处理
        for (Assignment assignment : finishedList) {
            assignment.setStatus(Constant.AssignmentStatus.HANDLED.getValue());
            assignmentFeignClient.update(assignment);
        }
    }

}
